var Node = require('./node');
var createEl = require('./create-el');
var evalExpr = require('../parser/expr-type');

function Element(options) {
    this.childs = [];
    this._elFns = {};
    Node.call(this, options);
}
Element.prototype.attach = function (parentEl, beforeEl) {
    if (!this.lifeCycle.is('attached')) {
        this._attach(parentEl, beforeEl);
        this._toAttached();
    }
}
Element.prototype._create = function () {
    var me = this;
    if (!this.el) {
        me.el = createEl(me.tagName);
        me.el.id = me.id;
        me.props.each(function (prop) {
            var value = isComponent(me) ?
                evalExpr(prop.expr, me.data, me) :
                me.evalExpr(prop.expr, 1);
            var match = /^\s+([a-z0-9_-]+)=(['"])([^\2]*)\2$/.exec(
                getPropHandler(me, prop.name)
                .input
                .attr(me, prop.name, value)
            );
            if (match) {
                me.el.setAttribute(match[1], match[3]);
            }
        })
    }
}
Element.prototype.create = function () {
    if (!this.lifeCycle.is('created')) {
        this._create();
        this._toPhase('created');
    }
}
Element.prototype._attach = function (parentEl, beforeEl) {
    this.create();
    this._toPhase('created');
};